package com.dragon.app.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.DeliverService;
import com.dragon.repository.domain.dto.DeliverDTO;
import com.dragon.repository.domain.dto.DeliverProductsDTO;
import com.dragon.repository.domain.vo.DeliverProductsVO;
import com.dragon.repository.domain.vo.DeliverVO;
import com.dragon.repository.entity.Deliver;
import com.dragon.repository.entity.DeliverProducts;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-06-26
 */
@RestController
@Api(tags = "送货")
@RequestMapping("deliver")
public class DeliverController {

    @Autowired
    DeliverService service;

    @GetMapping("list")
    @ApiOperation("获取送货列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, type = "string"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = false, type = "int"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, type = "int"),
    })
    public ResponseData<List<DeliverDTO>> deliverList(@RequestParam(required = false) String search,
                                                      @RequestParam(required = false) Integer status,
                                                      @RequestParam Integer page, @RequestParam Integer limit,
                                                      @RequestParam(required = false) Integer type){
        Page<Deliver> deliverPage = service.deliverList(search, status, page, limit,type);
        return ResponseData.success(DeliverDTO.convertList(deliverPage.getRecords()),deliverPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("生产单添加送货")
    @Log
    public ResponseData add(@RequestBody @Validated(Insert.class)DeliverVO vo ){
        Deliver deliver = DeliverVO.convert(vo);
        deliver.setDeliverProducts(DeliverProductsVO.convertList(vo.getDeliverProductsVO()));
        service.addDeliver(deliver);
        return ResponseData.success();
    }


    @PostMapping("addRejected")
    @ApiOperation("退货单添加送货")
    @Log
    public ResponseData addDeliver(@RequestBody @Validated(Insert.class)DeliverVO vo ){
        Deliver deliver = DeliverVO.convert(vo);
        deliver.setDeliverProducts(DeliverProductsVO.convertList(vo.getDeliverProductsVO()));
        service.addRejectedDeliver(deliver);
        return ResponseData.success();
    }


    @GetMapping("listById")
    @ApiOperation("根据id查看详情")
    public ResponseData<List<DeliverProductsDTO>> selectDeliverProductById(@RequestParam
                                          @ApiParam(name = "id", value = "id",required = true, type = "int") Integer id){
        List<DeliverProducts> deliverProducts = service.selectDeliverProductById(id);
        return ResponseData.success(DeliverProductsDTO.convertList(deliverProducts));
    }

    @PostMapping("listByDeliverId")
    @ApiOperation("根据送货id查询详情")
    public ResponseData listByDeliverId(Integer id){
        return ResponseData.success(service.listByDeliverId(id));
    }

    @PostMapping("updateStatus")
    @ApiOperation(value = "确认收货")
    public ResponseData changeDeliverStatus(@RequestBody DeliverVO vo){
        service.updateStatus(DeliverVO.convert(vo));
        return ResponseData.success();
    }

    @GetMapping("address")
    @ApiOperation(value = "获取客户地址")
    public ResponseData getAddress(@ApiParam(name = "contractId",required = true,type = "String",value = "合同id")@RequestParam Integer contractId){
        return ResponseData.success(service.selectCustomerAddressByContractId(contractId));
    }

    @GetMapping("informationOfDeliver")
    @ApiOperation(value = "合同总信息之送货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "contractId", value = "合同id", required = true, type = "int")
    })
    public ResponseData<List<DeliverDTO>> informationOfDeliver(
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam Integer contractId){
        Page<Deliver> deliverPage = service.informationOfDeliver(page, limit, contractId);
        return ResponseData.success(DeliverDTO.convertList(deliverPage.getRecords()));
    }
}
